<!DOCTYPE html><html lang="zh" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>vue3 快速搭建项目及基本配置 | 审陌</title><meta name="author" content="审陌"><meta name="copyright" content="审陌"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="vue3 搭建后台管理系统一、利用vite初始化项目1、创建项目终端键入如下代码 1yarn create vite  自定义项目名称，选择对应的前端框架（此处以vue为例）如果只是测试，依次回车即可 2、初始化结束之后，进入项目根目录，打开终端(文件目录栏键入cmd回车即可),键入如下命令安装依赖1yarn 或 yarn install  3、依赖安装完成后启动项目1yarn dev  4、启动">
<meta property="og:type" content="article">
<meta property="og:title" content="vue3 快速搭建项目及基本配置">
<meta property="og:url" content="https://gitee.com/trial-street/shenmo.git/2023/10/03/vue3/index.html">
<meta property="og:site_name" content="审陌">
<meta property="og:description" content="vue3 搭建后台管理系统一、利用vite初始化项目1、创建项目终端键入如下代码 1yarn create vite  自定义项目名称，选择对应的前端框架（此处以vue为例）如果只是测试，依次回车即可 2、初始化结束之后，进入项目根目录，打开终端(文件目录栏键入cmd回车即可),键入如下命令安装依赖1yarn 或 yarn install  3、依赖安装完成后启动项目1yarn dev  4、启动">
<meta property="og:locale">
<meta property="og:image" content="https://gitee.com/trial-street/shenmo.git/img/vue3.png">
<meta property="article:published_time" content="2023-10-02T16:24:00.000Z">
<meta property="article:modified_time" content="2023-10-30T11:45:08.377Z">
<meta property="article:author" content="审陌">
<meta property="article:tag" content="VUE3">
<meta property="article:tag" content="管理系统">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/trial-street/shenmo.git/img/vue3.png"><link rel="shortcut icon" href="/shenmo/img/favicon.png"><link rel="canonical" href="https://gitee.com/trial-street/shenmo.git/2023/10/03/vue3/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/shenmo/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/shenmo/',
  algolia: undefined,
  localSearch: {"path":"/shenmo/search.xml","preload":false,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy Successful',
    error: 'Copy Error',
    noSupport: 'Browser Not Supported'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: 'Just now',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: 'Load More'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'vue3 快速搭建项目及基本配置',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-10-30 19:45:08'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.2.0"><link rel="alternate" href="/shenmo/atom.xml" title="审陌" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/shenmo/img/fluid.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/shenmo/archives/"><div class="headline">Articles</div><div class="length-num">19</div></a><a href="/shenmo/tags/"><div class="headline">Tags</div><div class="length-num">19</div></a><a href="/shenmo/categories/"><div class="headline">Categories</div><div class="length-num">17</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/shenmo/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/shenmo/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/shenmo/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/shenmo/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 个人</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/shenmo/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/shenmo/movies/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/shenmo/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/shenmo/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg fixed" id="page-header" style="background-image: url('/shenmo/img/vue3.png')"><nav id="nav"><span id="blog-info"><a href="/shenmo/" title="审陌"><span class="site-name">审陌</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> Search</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/shenmo/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/shenmo/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/shenmo/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/shenmo/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 个人</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/shenmo/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/shenmo/movies/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/shenmo/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/shenmo/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">vue3 快速搭建项目及基本配置</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2023-10-02T16:24:00.000Z" title="Created 2023-10-03 00:24:00">2023-10-03</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2023-10-30T11:45:08.377Z" title="Updated 2023-10-30 19:45:08">2023-10-30</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/shenmo/categories/VUE3/">VUE3</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="vue3 快速搭建项目及基本配置"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post Views:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="vue3-搭建后台管理系统"><a href="#vue3-搭建后台管理系统" class="headerlink" title="vue3 搭建后台管理系统"></a>vue3 搭建后台管理系统</h1><h2 id="一、利用vite初始化项目"><a href="#一、利用vite初始化项目" class="headerlink" title="一、利用vite初始化项目"></a>一、利用vite初始化项目</h2><h3 id="1、创建项目"><a href="#1、创建项目" class="headerlink" title="1、创建项目"></a>1、创建项目</h3><p>终端键入如下代码</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn create vite</span><br></pre></td></tr></table></figure>

<p>自定义项目名称，选择对应的前端框架（此处以vue为例）如果只是测试，依次回车即可</p>
<h3 id="2、初始化结束之后，进入项目根目录，打开终端-文件目录栏键入cmd回车即可-键入如下命令安装依赖"><a href="#2、初始化结束之后，进入项目根目录，打开终端-文件目录栏键入cmd回车即可-键入如下命令安装依赖" class="headerlink" title="2、初始化结束之后，进入项目根目录，打开终端(文件目录栏键入cmd回车即可),键入如下命令安装依赖"></a>2、初始化结束之后，进入项目根目录，打开终端(文件目录栏键入cmd回车即可),键入如下命令安装依赖</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn 或 yarn install</span><br></pre></td></tr></table></figure>

<h3 id="3、依赖安装完成后启动项目"><a href="#3、依赖安装完成后启动项目" class="headerlink" title="3、依赖安装完成后启动项目"></a>3、依赖安装完成后启动项目</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn dev</span><br></pre></td></tr></table></figure>

<h3 id="4、启动成功后浏览器打开-http-localhost-5173-访问网站"><a href="#4、启动成功后浏览器打开-http-localhost-5173-访问网站" class="headerlink" title="4、启动成功后浏览器打开 http://localhost:5173  访问网站"></a>4、启动成功后浏览器打开 <a target="_blank" rel="noopener" href="http://localhost:5173/">http://localhost:5173</a>  访问网站</h3><h2 id="二、搭建路由"><a href="#二、搭建路由" class="headerlink" title="二、搭建路由"></a>二、搭建路由</h2><h3 id="1、安装路由"><a href="#1、安装路由" class="headerlink" title="1、安装路由"></a>1、安装路由</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add vue-router</span><br></pre></td></tr></table></figure>



<h3 id="2、src文件下新建router-x2F-index-js"><a href="#2、src文件下新建router-x2F-index-js" class="headerlink" title="2、src文件下新建router&#x2F;index.js"></a>2、src文件下新建router&#x2F;index.js</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">import &#123; createRouter, createWebHistory &#125; from &#x27;vue-router&#x27;</span><br><span class="line"></span><br><span class="line">const routes = [</span><br><span class="line">    &#123;</span><br><span class="line">        path: &quot;/&quot;,</span><br><span class="line">        name: &quot;login&quot;,</span><br><span class="line">        component: () =&gt; import(&quot;../views/login/index.vue&quot;)</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">        path: &quot;/register&quot;,</span><br><span class="line">        name: &quot;register&quot;,</span><br><span class="line">        component: () =&gt; import(&quot;../views/register/index.vue&quot;)</span><br><span class="line">    &#125;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">const router = createRouter(&#123;</span><br><span class="line">    history: createWebHistory(),</span><br><span class="line">    routes</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">export default router</span><br></pre></td></tr></table></figure>

<h3 id="3、引用路由"><a href="#3、引用路由" class="headerlink" title="3、引用路由"></a>3、引用路由</h3><p>main.js 文件中添加如下代码</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">import router from &quot;./router/index&quot;</span><br><span class="line"></span><br><span class="line">createApp(App).use(router).mount(&#x27;#app&#x27;)</span><br></pre></td></tr></table></figure>

<h3 id="4、app-vue文件修改为如下代码"><a href="#4、app-vue文件修改为如下代码" class="headerlink" title="4、app.vue文件修改为如下代码"></a>4、app.vue文件修改为如下代码</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;router-view /&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script setup&gt;</span><br><span class="line"></span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style scoped&gt;</span><br><span class="line"></span><br><span class="line">&lt;/style&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="三、页面跳转"><a href="#三、页面跳转" class="headerlink" title="三、页面跳转"></a>三、页面跳转</h2><h3 id="1、需要跳转页面引用如下代码"><a href="#1、需要跳转页面引用如下代码" class="headerlink" title="1、需要跳转页面引用如下代码"></a>1、需要跳转页面引用如下代码</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">import &#123; useRouter &#125; from &quot;vue-router&quot;</span><br><span class="line">const router = useRouter();</span><br><span class="line"></span><br><span class="line">const login = () =&gt; &#123;</span><br><span class="line">    router.push(&quot;/register&quot;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="四、安装element-plus组件"><a href="#四、安装element-plus组件" class="headerlink" title="四、安装element plus组件"></a>四、安装element plus组件</h3><h3 id="1、安装"><a href="#1、安装" class="headerlink" title="1、安装"></a>1、安装</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">yarn add element-plus</span><br><span class="line">yarn add -D unplugin-vue-components unplugin-auto-import</span><br></pre></td></tr></table></figure>

<h3 id="2、配置文件vite-config-js添加如下内容"><a href="#2、配置文件vite-config-js添加如下内容" class="headerlink" title="2、配置文件vite.config.js添加如下内容"></a>2、配置文件vite.config.js添加如下内容</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">import &#123; defineConfig &#125; from &#x27;vite&#x27;</span><br><span class="line">import AutoImport from &#x27;unplugin-auto-import/vite&#x27;</span><br><span class="line">import Components from &#x27;unplugin-vue-components/vite&#x27;</span><br><span class="line">import &#123; ElementPlusResolver &#125; from &#x27;unplugin-vue-components/resolvers&#x27;</span><br><span class="line"></span><br><span class="line">export default defineConfig(&#123;</span><br><span class="line">  // ...</span><br><span class="line">  plugins: [</span><br><span class="line">    // ...</span><br><span class="line">    AutoImport(&#123;</span><br><span class="line">      resolvers: [ElementPlusResolver()],</span><br><span class="line">    &#125;),</span><br><span class="line">    Components(&#123;</span><br><span class="line">      resolvers: [ElementPlusResolver()],</span><br><span class="line">    &#125;),</span><br><span class="line">  ],</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="3、配置完成后页面直接使用即可"><a href="#3、配置完成后页面直接使用即可" class="headerlink" title="3、配置完成后页面直接使用即可"></a>3、配置完成后页面直接使用即可</h3><h2 id="五、封装axios"><a href="#五、封装axios" class="headerlink" title="五、封装axios"></a>五、封装axios</h2><h3 id="1、安装-1"><a href="#1、安装-1" class="headerlink" title="1、安装"></a>1、安装</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add axios</span><br></pre></td></tr></table></figure>

<h3 id="2、项目根目录新建文件-env-dev-env-prod-分别键入内容"><a href="#2、项目根目录新建文件-env-dev-env-prod-分别键入内容" class="headerlink" title="2、项目根目录新建文件.env.dev ,  .env.prod, 分别键入内容"></a>2、项目根目录新建文件.env.dev ,  .env.prod, 分别键入内容</h3><p>.env.dev(开发环境配置)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">NODE_ENV = &quot;development&quot;</span><br><span class="line">BASE_URL = 	&quot;http://localhost:3000&quot;</span><br></pre></td></tr></table></figure>

<p>.env.prod(生产环境配置)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">NODE_DEV = &quot;production&quot;</span><br><span class="line">BASE_URL = &quot;http://localhost:3000&quot;</span><br><span class="line"></span><br><span class="line"># 如下仅供打样</span><br><span class="line">————————————————————————————————————————————————————————————————————————————————————</span><br><span class="line"># 生产环境配置</span><br><span class="line">ENV = &#x27;production&#x27;</span><br><span class="line"></span><br><span class="line"># 生产环境</span><br><span class="line">VITE_APP_BASE_API = &#x27;/prod-api&#x27;</span><br><span class="line"></span><br><span class="line"># 路由前缀</span><br><span class="line">VITE_APP_ROUTER_PREFIX = &#x27;/&#x27;</span><br><span class="line"></span><br><span class="line"># 默认上传地址</span><br><span class="line">VITE_APP_UPLOAD_URL = &#x27;/Common/UploadFile&#x27;</span><br><span class="line"></span><br><span class="line">#socket API</span><br><span class="line">VITE_APP_SOCKET_API = &#x27;/msghub&#x27;</span><br></pre></td></tr></table></figure>

<p>该配置可在package.json中进行配置，一键切换</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&quot;scripts&quot;: &#123;</span><br><span class="line">	&quot;dev&quot;: &quot;vite&quot;,</span><br><span class="line">    &quot;pro&quot;: &quot;vite --mode production&quot;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3、src文件下新建request-x2F-index-js"><a href="#3、src文件下新建request-x2F-index-js" class="headerlink" title="3、src文件下新建request&#x2F;index.js"></a>3、src文件下新建request&#x2F;index.js</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">import axios from &quot;axios&quot;;</span><br><span class="line"> </span><br><span class="line">//不同环境下的地址</span><br><span class="line">let baseURL = &quot;&quot;;</span><br><span class="line">//node中的环境变量process.env,也就是我们新增开发、生产的配置文件</span><br><span class="line">if(process.env.NODE_ENV === &quot;development&quot;)&#123;</span><br><span class="line">    baseURL = &quot;http://localhost:8080&quot;  //这里可在vue.config.js做一个代理【代理代码见下】</span><br><span class="line">&#125;else&#123;</span><br><span class="line">    baseURL = &quot;123.com.cn&quot;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line">//创建axios实例</span><br><span class="line">const request = axios.create(&#123;</span><br><span class="line">    timeout: 5000,</span><br><span class="line">    baseURL:baseURL</span><br><span class="line">&#125;);</span><br><span class="line"> </span><br><span class="line">//所有请求设置了授权头信息【请求头中设置cookie信息，一直携带，判权】</span><br><span class="line">axios.defaults.headers.common[&#x27;Authorization&#x27;] = `$&#123;cookie&#125;`;</span><br><span class="line">//这里是设置发送json格式参数</span><br><span class="line">axios.defaults.headers.post[&#x27;Content-Type&#x27;] = &#x27;application/json&#x27;;</span><br><span class="line"> </span><br><span class="line">//请求拦截器</span><br><span class="line">request.interceptors.request.use(</span><br><span class="line">    //发送之前想要做些什么</span><br><span class="line">    config =&gt; &#123;</span><br><span class="line">        // token是否过期，是否重新登陆等等</span><br><span class="line">         //if (store.getters.token) &#123;</span><br><span class="line">          //config.headers[&#x27;X-Token&#x27;] = getToken()</span><br><span class="line">       // &#125;</span><br><span class="line">        // 此处可修改一些基本数据</span><br><span class="line">        // config[baseURL,[data,[headers,[method,[timeout,[url]   等</span><br><span class="line">        //        基础地址,请求参数,头部, 请求方式, 超时,  请求地址  等</span><br><span class="line"> </span><br><span class="line">        // config.headers.Cookie = &quot;110110110110110110&quot;;</span><br><span class="line">        return config;</span><br><span class="line">    &#125;,</span><br><span class="line">    //方法返回一个带有拒绝原因的 Promise 对象。</span><br><span class="line">    error =&gt; Promise.reject(error)</span><br><span class="line">);</span><br><span class="line">/**************移除拦截器****************/</span><br><span class="line">    // 动态移除</span><br><span class="line">    // const me = axios.interceptors.request.use(function () &#123;/*...*/&#125;);</span><br><span class="line">    // axios.interceptors.request.eject(me);</span><br><span class="line">/***************************************/</span><br><span class="line"> </span><br><span class="line">//响应拦截器（对请求结束后进行一些操作，，例如:统一收集报错）</span><br><span class="line">request.interceptors.response.use(</span><br><span class="line">    //请求成功</span><br><span class="line">    res =&gt; res.status === 200 ? Promise.resolve(res) : Promise.reject(res),</span><br><span class="line">    //请求失败</span><br><span class="line">    error =&gt; &#123;</span><br><span class="line">        //可根据不同的状态去区分不同的错误，达到不同效果</span><br><span class="line">        if(error.response.status)&#123;</span><br><span class="line">            error.response.status === 404 ? alert(&quot;请求不存在！！&quot;) : alert(&quot;其他&quot;);</span><br><span class="line">        &#125;</span><br><span class="line">        return Promise.reject(error);</span><br><span class="line">    &#125;</span><br><span class="line">);</span><br><span class="line"> </span><br><span class="line">export default request</span><br></pre></td></tr></table></figure>

<h3 id="4、代理配置"><a href="#4、代理配置" class="headerlink" title="4、代理配置"></a>4、代理配置</h3><p>vite.config.js添加如下代码</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">export default defineConfig(&#123;</span><br><span class="line">   // 其他配置</span><br><span class="line">  ...... </span><br><span class="line">  // 代理配置</span><br><span class="line">  server: &#123;</span><br><span class="line">  	https: true,</span><br><span class="line">    proxy: &#123;</span><br><span class="line">      &#x27;/api&#x27;: &#123; // 配置需要代理的路径 --&gt; 这里的意思是代理http://localhost:80/api/后的所有路由</span><br><span class="line">        target: &#x27;https://172.20.9.153:8085&#x27;, // 目标地址 --&gt; 服务器地址</span><br><span class="line">        changeOrigin: true, // 允许跨域</span><br><span class="line">        ws: true,  // 允许websocket代理</span><br><span class="line">		// 重写路径 --&gt; 作用与vue配置pathRewrite作用相同</span><br><span class="line">        rewrite: (path) =&gt; path.replace(/^\/api/, &quot;&quot;)</span><br><span class="line">      &#125;</span><br><span class="line">     &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>注意：配置代理的时候’&#x2F;api’如果直接写成‘ &#x2F;’,这样表示<a target="_blank" rel="noopener" href="http://localhost/%E5%90%8E%E9%9D%A2%E7%9A%84%E8%B7%AF%E7%94%B1%E9%83%BD%E8%BF%9B%E8%A1%8C%E4%BB%A3%E7%90%86%EF%BC%8C%E8%BF%99%E6%A0%B7%E4%BC%9A%E5%AF%BC%E8%87%B4%E4%BD%A0%E5%8A%A0%E8%BD%BD%E6%9C%AC%E5%9C%B0%E8%B5%84%E6%BA%90%E4%BC%9A%E5%87%BA%E9%94%99%EF%BC%8C%E5%9B%A0%E4%B8%BA%E4%BD%A0%E6%8A%8A%E5%8A%A0%E8%BD%BD%E6%9C%AC%E5%9C%B0%E8%B5%84%E6%BA%90%E7%9A%84%E8%B7%AF%E5%BE%84%E5%85%A8%E9%83%A8%E9%83%BD%E4%BB%A3%E7%90%86%E5%88%B0%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%8E%BB%E4%BA%86%EF%BC%8C%E6%B5%8F%E8%A7%88%E5%99%A8%E4%BC%9A%E5%90%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%9B%E8%A1%8C%E8%B5%84%E6%BA%90%E8%AF%B7%E6%B1%82%EF%BC%8C%E8%BF%99%E6%A0%B7%E5%B0%B1%E4%BC%9A%E5%AF%BC%E8%87%B4%E9%A1%B5%E9%9D%A2%E6%8A%A5%E9%94%99">http://localhost:80/后面的路由都进行代理，这样会导致你加载本地资源会出错，因为你把加载本地资源的路径全部都代理到服务端去了，浏览器会向服务器进行资源请求，这样就会导致页面报错</a></p>
<h2 id="六、pinia"><a href="#六、pinia" class="headerlink" title="六、pinia"></a>六、pinia</h2><h3 id="1、安装-2"><a href="#1、安装-2" class="headerlink" title="1、安装"></a>1、安装</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add pinia</span><br></pre></td></tr></table></figure>

<h3 id="2、封装"><a href="#2、封装" class="headerlink" title="2、封装"></a>2、封装</h3><p>新建页面store&#x2F;index.js</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">import &#123; defineStore &#125; from &quot;pinia&quot;;</span><br><span class="line"></span><br><span class="line">// defineStore 调用后返回一个函数，调用该函数获得 Store 实体</span><br><span class="line">export const useStore = defineStore(&#123;</span><br><span class="line">  // id: 必须的，在所有 Store 中唯一</span><br><span class="line">  id: &quot;myGlobalState&quot;,</span><br><span class="line">  // state: 返回对象的函数</span><br><span class="line">  state: ()=&gt; (&#123;</span><br><span class="line">    count: 1</span><br><span class="line">  &#125;),</span><br><span class="line">   getters: &#123;</span><br><span class="line">    // 一个基本的 Getter： 计算 count 的平方</span><br><span class="line">    // 使用参数</span><br><span class="line">    countPow2(state) &#123;</span><br><span class="line">      return state.count ** 2;</span><br><span class="line">    &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">  actinos: &#123;</span><br><span class="line">    countPlusOne() &#123;</span><br><span class="line">      this.count++;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="3、全局挂载"><a href="#3、全局挂载" class="headerlink" title="3、全局挂载"></a>3、全局挂载</h3><p>main.js中新增</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">import &#123; createPinia &#125; from &#x27;pinia&#x27;;</span><br><span class="line"></span><br><span class="line">app.use(createPinia());</span><br></pre></td></tr></table></figure>

<h3 id="4、组件中使用"><a href="#4、组件中使用" class="headerlink" title="4、组件中使用"></a>4、组件中使用</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">import &#123; useStore,storeToRefs  &#125; from &quot;@/store/index.js&quot;;</span><br><span class="line">const store = useStore()</span><br><span class="line">//响应式</span><br><span class="line">const &#123; count &#125; = storeToRefs(store)</span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>Author: </span><span class="post-copyright-info"><a href="https://gitee.com/trial-street/shenmo.git">审陌</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>Link: </span><span class="post-copyright-info"><a href="https://gitee.com/trial-street/shenmo.git/2023/10/03/vue3/">https://gitee.com/trial-street/shenmo.git/2023/10/03/vue3/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/shenmo/tags/VUE3/">VUE3</a><a class="post-meta__tags" href="/shenmo/tags/%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/">管理系统</a></div><div class="post_share"><div class="social-share" data-image="/shenmo/img/vue3.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/shenmo/2023/10/03/FigureBed/" title="服务器搭建图床"><img class="cover" src="/shenmo/img/imgs.png" onerror="onerror=null;src='/shenmo/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous</div><div class="prev_info">服务器搭建图床</div></div></a></div><div class="next-post pull-right"><a href="/shenmo/2023/02/11/vue/" title="vue组件传值"><img class="cover" src="/shenmo/img/VUE.png" onerror="onerror=null;src='/shenmo/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next</div><div class="next_info">vue组件传值</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>Related Articles</span></div><div class="relatedPosts-list"><div><a href="/shenmo/2023/10/27/vue3count/" title="vue3 问题合集"><img class="cover" src="/shenmo/img/vue3.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-10-27</div><div class="title">vue3 问题合集</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/shenmo/img/fluid.png" onerror="this.onerror=null;this.src='/shenmo/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">审陌</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/shenmo/archives/"><div class="headline">Articles</div><div class="length-num">19</div></a><a href="/shenmo/tags/"><div class="headline">Tags</div><div class="length-num">19</div></a><a href="/shenmo/categories/"><div class="headline">Categories</div><div class="length-num">17</div></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Contents</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#vue3-%E6%90%AD%E5%BB%BA%E5%90%8E%E5%8F%B0%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F"><span class="toc-number">1.</span> <span class="toc-text">vue3 搭建后台管理系统</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E5%88%A9%E7%94%A8vite%E5%88%9D%E5%A7%8B%E5%8C%96%E9%A1%B9%E7%9B%AE"><span class="toc-number">1.1.</span> <span class="toc-text">一、利用vite初始化项目</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E5%88%9B%E5%BB%BA%E9%A1%B9%E7%9B%AE"><span class="toc-number">1.1.1.</span> <span class="toc-text">1、创建项目</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81%E5%88%9D%E5%A7%8B%E5%8C%96%E7%BB%93%E6%9D%9F%E4%B9%8B%E5%90%8E%EF%BC%8C%E8%BF%9B%E5%85%A5%E9%A1%B9%E7%9B%AE%E6%A0%B9%E7%9B%AE%E5%BD%95%EF%BC%8C%E6%89%93%E5%BC%80%E7%BB%88%E7%AB%AF-%E6%96%87%E4%BB%B6%E7%9B%AE%E5%BD%95%E6%A0%8F%E9%94%AE%E5%85%A5cmd%E5%9B%9E%E8%BD%A6%E5%8D%B3%E5%8F%AF-%E9%94%AE%E5%85%A5%E5%A6%82%E4%B8%8B%E5%91%BD%E4%BB%A4%E5%AE%89%E8%A3%85%E4%BE%9D%E8%B5%96"><span class="toc-number">1.1.2.</span> <span class="toc-text">2、初始化结束之后，进入项目根目录，打开终端(文件目录栏键入cmd回车即可),键入如下命令安装依赖</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E4%BE%9D%E8%B5%96%E5%AE%89%E8%A3%85%E5%AE%8C%E6%88%90%E5%90%8E%E5%90%AF%E5%8A%A8%E9%A1%B9%E7%9B%AE"><span class="toc-number">1.1.3.</span> <span class="toc-text">3、依赖安装完成后启动项目</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%E3%80%81%E5%90%AF%E5%8A%A8%E6%88%90%E5%8A%9F%E5%90%8E%E6%B5%8F%E8%A7%88%E5%99%A8%E6%89%93%E5%BC%80-http-localhost-5173-%E8%AE%BF%E9%97%AE%E7%BD%91%E7%AB%99"><span class="toc-number">1.1.4.</span> <span class="toc-text">4、启动成功后浏览器打开 http:&#x2F;&#x2F;localhost:5173  访问网站</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E6%90%AD%E5%BB%BA%E8%B7%AF%E7%94%B1"><span class="toc-number">1.2.</span> <span class="toc-text">二、搭建路由</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E5%AE%89%E8%A3%85%E8%B7%AF%E7%94%B1"><span class="toc-number">1.2.1.</span> <span class="toc-text">1、安装路由</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81src%E6%96%87%E4%BB%B6%E4%B8%8B%E6%96%B0%E5%BB%BArouter-x2F-index-js"><span class="toc-number">1.2.2.</span> <span class="toc-text">2、src文件下新建router&#x2F;index.js</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E5%BC%95%E7%94%A8%E8%B7%AF%E7%94%B1"><span class="toc-number">1.2.3.</span> <span class="toc-text">3、引用路由</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%E3%80%81app-vue%E6%96%87%E4%BB%B6%E4%BF%AE%E6%94%B9%E4%B8%BA%E5%A6%82%E4%B8%8B%E4%BB%A3%E7%A0%81"><span class="toc-number">1.2.4.</span> <span class="toc-text">4、app.vue文件修改为如下代码</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E9%A1%B5%E9%9D%A2%E8%B7%B3%E8%BD%AC"><span class="toc-number">1.3.</span> <span class="toc-text">三、页面跳转</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E9%9C%80%E8%A6%81%E8%B7%B3%E8%BD%AC%E9%A1%B5%E9%9D%A2%E5%BC%95%E7%94%A8%E5%A6%82%E4%B8%8B%E4%BB%A3%E7%A0%81"><span class="toc-number">1.3.1.</span> <span class="toc-text">1、需要跳转页面引用如下代码</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E5%AE%89%E8%A3%85element-plus%E7%BB%84%E4%BB%B6"><span class="toc-number">1.3.2.</span> <span class="toc-text">四、安装element plus组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E5%AE%89%E8%A3%85"><span class="toc-number">1.3.3.</span> <span class="toc-text">1、安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6vite-config-js%E6%B7%BB%E5%8A%A0%E5%A6%82%E4%B8%8B%E5%86%85%E5%AE%B9"><span class="toc-number">1.3.4.</span> <span class="toc-text">2、配置文件vite.config.js添加如下内容</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E9%85%8D%E7%BD%AE%E5%AE%8C%E6%88%90%E5%90%8E%E9%A1%B5%E9%9D%A2%E7%9B%B4%E6%8E%A5%E4%BD%BF%E7%94%A8%E5%8D%B3%E5%8F%AF"><span class="toc-number">1.3.5.</span> <span class="toc-text">3、配置完成后页面直接使用即可</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E5%B0%81%E8%A3%85axios"><span class="toc-number">1.4.</span> <span class="toc-text">五、封装axios</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E5%AE%89%E8%A3%85-1"><span class="toc-number">1.4.1.</span> <span class="toc-text">1、安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81%E9%A1%B9%E7%9B%AE%E6%A0%B9%E7%9B%AE%E5%BD%95%E6%96%B0%E5%BB%BA%E6%96%87%E4%BB%B6-env-dev-env-prod-%E5%88%86%E5%88%AB%E9%94%AE%E5%85%A5%E5%86%85%E5%AE%B9"><span class="toc-number">1.4.2.</span> <span class="toc-text">2、项目根目录新建文件.env.dev ,  .env.prod, 分别键入内容</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81src%E6%96%87%E4%BB%B6%E4%B8%8B%E6%96%B0%E5%BB%BArequest-x2F-index-js"><span class="toc-number">1.4.3.</span> <span class="toc-text">3、src文件下新建request&#x2F;index.js</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%E3%80%81%E4%BB%A3%E7%90%86%E9%85%8D%E7%BD%AE"><span class="toc-number">1.4.4.</span> <span class="toc-text">4、代理配置</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AD%E3%80%81pinia"><span class="toc-number">1.5.</span> <span class="toc-text">六、pinia</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E5%AE%89%E8%A3%85-2"><span class="toc-number">1.5.1.</span> <span class="toc-text">1、安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81%E5%B0%81%E8%A3%85"><span class="toc-number">1.5.2.</span> <span class="toc-text">2、封装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E5%85%A8%E5%B1%80%E6%8C%82%E8%BD%BD"><span class="toc-number">1.5.3.</span> <span class="toc-text">3、全局挂载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%E3%80%81%E7%BB%84%E4%BB%B6%E4%B8%AD%E4%BD%BF%E7%94%A8"><span class="toc-number">1.5.4.</span> <span class="toc-text">4、组件中使用</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/shenmo/2023/10/30/flySearch/" title="ButterFly 主题"><img src="/shenmo/img/butterfly.png" onerror="this.onerror=null;this.src='/shenmo/img/404.jpg'" alt="ButterFly 主题"/></a><div class="content"><a class="title" href="/shenmo/2023/10/30/flySearch/" title="ButterFly 主题">ButterFly 主题</a><time datetime="2023-10-30T13:24:00.000Z" title="Created 2023-10-30 21:24:00">2023-10-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/shenmo/2023/10/27/css/" title="CSS 案例合集"><img src="/shenmo/img/css.png" onerror="this.onerror=null;this.src='/shenmo/img/404.jpg'" alt="CSS 案例合集"/></a><div class="content"><a class="title" href="/shenmo/2023/10/27/css/" title="CSS 案例合集">CSS 案例合集</a><time datetime="2023-10-27T13:24:00.000Z" title="Created 2023-10-27 21:24:00">2023-10-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/shenmo/2023/10/27/echarts/" title="Echarts"><img src="/shenmo/img/echarts.png" onerror="this.onerror=null;this.src='/shenmo/img/404.jpg'" alt="Echarts"/></a><div class="content"><a class="title" href="/shenmo/2023/10/27/echarts/" title="Echarts">Echarts</a><time datetime="2023-10-27T13:24:00.000Z" title="Created 2023-10-27 21:24:00">2023-10-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/shenmo/2023/10/27/js/" title="JS API合集"><img src="/shenmo/img/js.png" onerror="this.onerror=null;this.src='/shenmo/img/404.jpg'" alt="JS API合集"/></a><div class="content"><a class="title" href="/shenmo/2023/10/27/js/" title="JS API合集">JS API合集</a><time datetime="2023-10-27T13:24:00.000Z" title="Created 2023-10-27 21:24:00">2023-10-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/shenmo/2023/10/27/vue3count/" title="vue3 问题合集"><img src="/shenmo/img/vue3.png" onerror="this.onerror=null;this.src='/shenmo/img/404.jpg'" alt="vue3 问题合集"/></a><div class="content"><a class="title" href="/shenmo/2023/10/27/vue3count/" title="vue3 问题合集">vue3 问题合集</a><time datetime="2023-10-27T13:24:00.000Z" title="Created 2023-10-27 21:24:00">2023-10-27</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By 审陌</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Toggle Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between Single-column and Double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/shenmo/js/utils.js"></script><script src="/shenmo/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">Search</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  Loading the Database</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="Search for Posts" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/shenmo/js/search/local-search.js"></script></div></div></body></html>